package top.chengdongqing.mishop.repositories

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import top.chengdongqing.mishop.entities.OrderItem

interface OrderItemRepository : JpaRepository<OrderItem, Int> {

    @Query(
        """
        FROM OrderItem oi
        WHERE oi.id IN (
            SELECT MAX(id)
            FROM OrderItem
            WHERE order.id = :orderId
            GROUP BY productId
        )
    """
    )
    fun findAllByOrderIdGroupByProductId(orderId: Int): List<OrderItem>
}